package com.stadium.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.stadium.common.exception.RRException;
import com.stadium.common.utils.SqlMapper;
import com.stadium.common.utils.UserUtils;
import com.stadium.dto.GamePlayerDto;
import com.stadium.dto.SysAdminUserDto;
import com.stadium.dto.SysUserDto;
import com.stadium.entity.SysAdminUser;
import com.stadium.mapper.GameEventMP;
import com.stadium.mapper.GamePlayerMP;
import com.stadium.mapper.SysAdminUserMP;
import com.stadium.service.SysAdminUserSV;
import com.stadium.service.SysUserSV;
import com.stadium.vo.SysAdminUserVo;
import com.stadium.vo.SysUserVo;
import com.stadium.vo.searchVo.GameEventSearchVo;
import com.stadium.vo.searchVo.SysAdminUserSearchVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SysAdminUserSVI implements SysAdminUserSV {
    @Autowired
    private SysAdminUserMP mapper;
    @Autowired
    private SqlMapper sqlMapper;
    @Autowired
    private SysUserSV sysUserSV;
    @Autowired
    private GameEventMP gameEventMP;
    @Autowired
    private GamePlayerMP gamePlayerMP;

    @Override
    public List<SysAdminUser> queryBySQL(String sql, Map<String, Object> map) {
        return sqlMapper.selectList(sql, map, SysAdminUser.class);
    }

    @Override
    public void save(SysAdminUserVo sysAdminUserVo) {
        int apply = mapper.isApply(sysAdminUserVo.getUserId());
        if (apply > 0) {
            return;
        }
        SysAdminUser sysAdminUser = new SysAdminUser();
        BeanUtils.copyProperties(sysAdminUserVo, sysAdminUser);
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        sysAdminUser.setCreateUser(UserUtils.getUserId());
        sysAdminUser.setUpdateUser(UserUtils.getUserId());
        sysAdminUser.setId(id);
        if(sysAdminUserVo.getAuditStatus() == null ){
            sysAdminUser.setAuditStatus("2");
        }
        GamePlayerDto byUserId = gamePlayerMP.getByUserId(sysAdminUserVo.getUserId());
        if (byUserId != null) {
            sysAdminUser.setArea(byUserId.getArea());
            sysAdminUser.setRegisterTime(new Date());
        }
        mapper.save(sysAdminUser);
    }

    @Override
    public void update(SysAdminUserVo sysAdminUserVo) {
        SysAdminUser sysAdminUser = new SysAdminUser();
        BeanUtils.copyProperties(sysAdminUserVo, sysAdminUser);
        sysAdminUser.setUpdateUser(UserUtils.getUserId());
        mapper.update(sysAdminUser);
    }

    @Override
    public void saveBatch(List<SysAdminUser> list) {
        mapper.saveBatch(list);
    }

    @Override
    public void delete(String id) {
        mapper.delete(id);
    }

    @Override
    public void deleteBatch(String[] ids) {
        mapper.deleteBatch(ids);
    }

    @Override
    public List<SysAdminUserDto> query(SysAdminUserSearchVo searchVo) {
        List<SysAdminUserDto> query = mapper.query(searchVo);
        for (SysAdminUserDto sysAdminUserDto : query) {
            GameEventSearchVo gameEventSearchVo = new GameEventSearchVo();
            gameEventSearchVo.setUserId(sysAdminUserDto.getUserId());
            int count = gameEventMP.getCount(gameEventSearchVo);
            sysAdminUserDto.setCreateNum(count);
        }
        return query;
    }

    @Override
    public SysAdminUserDto get(String id) {
        return mapper.get(id);
    }

    @Override
    public int getCount(SysAdminUserSearchVo searchVo) {
        return mapper.getCount(searchVo);
    }

    @Override
    public void audit(String id, String status) {
        SysAdminUserDto sysAdminUserDto = mapper.get(id);
        SysAdminUser sysAdminUser = new SysAdminUser();
        if (sysAdminUserDto == null) {
            throw  new RRException("用户不存在");
        }
        if ("1".equals(status)) {
            SysUserVo sysUserVo = new SysUserVo();
            sysUserVo.setId(sysAdminUserDto.getUserId());
            sysUserVo.setIsAdmin("1");
            sysUserSV.update(sysUserVo);
        }
        if ("2".equals(status)) {
            SysUserVo sysUserVo = new SysUserVo();
            sysUserVo.setId(sysAdminUserDto.getUserId());
            sysUserVo.setIsAdmin("0");
            sysUserSV.update(sysUserVo);
            sysAdminUser.setDeleteCode(id);
        }

        sysAdminUser.setId(id);
        sysAdminUser.setAuditStatus(status);
        sysAdminUser.setUpdateUser(UserUtils.getUserId());
        mapper.update(sysAdminUser);
    }

}
